package com.ll.wechat.base.activity;

import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.ll.wechat.R;
import com.ll.wechat.base.app.Constant;
import com.ll.wechat.base.net.NetCallBackListener;
import com.ll.wechat.base.net.NetUtil;
import com.ll.wechat.base.utils.widget.LoadingDialog;
import com.ll.wechat.base.utils.widget.LoginEditext;
import com.ll.wechat.base.utils.widget.MyToast;

/**
 * 登陆界面
 * 
 * @ClassName: LoginActivity
 * @author yeliangliang
 * @date 2015-7-22 下午2:49:07
 */
public class LoginActivity extends BaseActivity implements OnClickListener {
	private Button btn_login;
	private LoginEditext edt_passName;
	private LoginEditext edt_passWord;
	private String passName = "";
	private String passWord = "";
	private LoadingDialog dialog;// 请求对话框
	private TextView tv_register;// 注册按钮

	@Override
	public void initView() {
		setContentView(R.layout.activity_login);
		btn_login = (Button) findViewById(R.id.btn_login);
		edt_passName = (LoginEditext) findViewById(R.id.edt_passName);
		edt_passWord = (LoginEditext) findViewById(R.id.edt_passWord);
		tv_register = (TextView) findViewById(R.id.tv_register);
		tv_register.setOnClickListener(this);
		btn_login.setOnClickListener(this);
		// 取消actionBar的图标
		getActionBar().setDisplayShowHomeEnabled(false);
	}

	@Override
	public void initData() {
		// showActivity(LoginActivity.this, MainActivity.class);
		// loginRequest();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_login:
			// 登录逻辑
			passName = edt_passName.getText().toString();
			passWord = edt_passWord.getText().toString();
			if (TextUtils.isEmpty(passName.trim()) || TextUtils.isEmpty(passWord.trim())) {
				MyToast.showToast(LoginActivity.this, "请输入账号密码");
				return;
			}
			// 判断网络状态
			if (!NetUtil.isNetConnection()) {
				MyToast.showToast(LoginActivity.this, R.string.net_connection_failed);
				return;
			}
			if (dialog == null) {
				dialog = new LoadingDialog(LoginActivity.this,"正在登录");
			}
			dialog.showDialog();
			loginRequest();
			break;
		case R.id.tv_register:
			// 注册页面
			showActivity(LoginActivity.this, RegisterActivity.class);
			break;
		default:
			break;
		}
	}

	/**
	 * 登录请求
	 * 
	 * @author yeliangliang
	 * @date 2015-7-22 下午4:17:50
	 * @version V1.0
	 * @return void
	 */
	private void loginRequest() {
		NetUtil.requestLogin(this, passName, passWord, new NetCallBackListener() {
			@Override
			public void responseSuccess(int i, Object o) {
				if (dialog != null) {
					dialog.cancel();
				}
				if (i == Constant.REQUEST_SUCCESS) {
					// 登录成功
					// 跳转到主页
					showActivity(LoginActivity.this, MainActivity.class);
					finishActivity(LoginActivity.this);
				}
			}

			@Override
			public void responseFailed(int i) {
				if (dialog != null) {
					dialog.cancel();
				}
				handler.obtainMessage(i).sendToTarget();
			}

			@Override
			public void responseError(int i) {
				if (dialog != null) {
					dialog.cancel();
				}
				handler.obtainMessage(i).sendToTarget();
			}

		});

	}

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case Constant.REQUEST_FAILD:
				MyToast.showToast(LoginActivity.this, R.string.login_error);
				break;
			case Constant.LOGIN_PASSWORD_ERROR:
				MyToast.showToast(LoginActivity.this, R.string.login_password_error);
				break;
			default:
				break;
			}
		}

	};

	// 返回键的监听
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
			exitApp(LoginActivity.this);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}
